import axios from 'axios'
import type { WmcRequestInterceptors, WmcRequestConfig } from './type'
import type { AxiosInstance } from 'axios'

import { ElLoading } from 'element-plus/lib/components/loading/index'
// import { LoadingOptionsResolved } from 'element-plus/lib/components/loading/src/'
// import LoadingInstance from 'element-plus/lib/components/loading'

const DEFAULT_LOADING = true

class WmcRequest {
  instance: AxiosInstance
  interceptors?: WmcRequestInterceptors
  showLoading: boolean
  loading?: any

  constructor(config: WmcRequestConfig) {
    this.instance = axios.create(config)
    this.interceptors = config.interceptors
    this.showLoading = config.showLoading ?? DEFAULT_LOADING

    // 创建实例时，添加实例拦截器
    this.instance.interceptors.request.use(
      this.interceptors?.requestInterceptorCatch,
      this.interceptors?.responseInterceptorCatch
    )

    this.instance.interceptors.response.use(
      this.interceptors?.responseInterceptors,
      this.interceptors?.responseInterceptorCatch
    )

    // 创建实例时，添加全局拦截器
    this.instance.interceptors.request.use(
      (config) => {
        if (this.showLoading) {
          this.loading = ElLoading.service({
            lock: true,
            text: '正在请求数据...',
            background: 'rgba(0, 0, 0, 0.5)'
          })
        }
        return config
      },
      (err) => {
        return err
      }
    )

    this.instance.interceptors.response.use(
      (res) => {
        this.loading?.close()

        const data = res.data
        if (data.code === '-1001') {
          console.log('请求失败，错误信息')
        } else {
          return data
        }
      },
      (err) => {
        this.loading?.close()
        if (err.response.status === 404) {
          console.log('404错误')
        }
        return err
      }
    )
  }

  request<T>(config: WmcRequestConfig<T>): Promise<T> {
    return new Promise((resolve, reject) => {
      // 执行请求拦截器
      if (config.interceptors?.requestInterceptors) {
        config = config.interceptors.requestInterceptors(config)
        console.log(config)
      }

      // 处理loading
      if (config.showLoading === false) {
        this.showLoading = false
      }

      // 执行request请求
      this.instance
        .request<any, T>(config)
        .then((res) => {
          // 执行响应拦截器
          if (config.interceptors?.responseInterceptors) {
            res = config.interceptors.responseInterceptors(res)
          }

          this.showLoading = DEFAULT_LOADING

          resolve(res)
        })
        .catch((err) => {
          this.showLoading = DEFAULT_LOADING
          reject(err)
          return err
        })
    })
  }

  get<T>(config: WmcRequestConfig<T>): Promise<T> {
    return this.request<T>({
      ...config,
      method: 'GET'
    })
  }

  post<T>(config: WmcRequestConfig<T>): Promise<T> {
    return this.request<T>({
      ...config,
      method: 'POST'
    })
  }

  delete<T>(config: WmcRequestConfig<T>): Promise<T> {
    return this.request<T>({
      ...config,
      method: 'DELETE'
    })
  }

  patch<T>(config: WmcRequestConfig<T>): Promise<T> {
    return this.request<T>({
      ...config,
      method: 'PATCH'
    })
  }
}

export default WmcRequest
